﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;

namespace JQuery.Net.UI
{
    public class jQNetResults
    {
        /// <summary>
        /// Get or Set the Data Type of the data being returned to the client.
        /// </summary>
        public JQNDataTypes type { get; set; }
        /// <summary>
        /// Get or Set the Data that is being returned to the client.
        /// </summary>
        public object data { get; set; }
    }

    public class jQNetEventArgs
        : EventArgs
    {
        #region private data
        protected Page oPage = null;
        #endregion

        #region constructors
        public jQNetEventArgs()
            : base()
        {
            oPage = HttpContext.Current.Handler as Page;
            Cancel = false;
        }
        #endregion

        #region properties
        /// <summary>
        /// get a flag that indicates that this event was triggered via callback.
        /// </summary>
        public bool IsCallback { get { return oPage.IsCallback; } }
        /// <summary>
        /// Get or Set if this event is canceled. default is false;
        /// </summary>
        public bool Cancel { get; set; }
        #endregion
    }

    public class jQNetArguments
        : jQNetEventArgs
    {
        public jQNetArguments()
            : base()
        {
            Event = jQNetEvents.unknown;

            ResultData = new jQNetResults()
            {
                type = JQNDataTypes.json,
                data = String.Empty
            };
        }
        /// <summary>
        /// parse the argmuments passed in JSON text
        /// </summary>
        /// <param name="arguments">arguments in json text</param>
        public jQNetArguments(string arguments)
            : this()
        {
            Arguments = (JObject)JsonConvert.DeserializeObject(arguments);
        }

        public object this[string key]
        {
            get
            {
                return Arguments[key];
            }
        }

        /// <summary>
        /// Get or Set the JQuery Event Trigger
        /// </summary>
        public jQNetEvents Event { get; set; }
        /// <summary>
        /// Get or Set the ICallbackEventHandler event arguments
        /// </summary>
        [JsonIgnore]
        protected JObject Arguments { get; set; }
        /// <summary>
        /// Get or Set the ResultData to be sent to the client.
        /// </summary>
        public jQNetResults ResultData { get; set; }
        /// <summary>
        /// Get or Set a flag that indicates that the event was handled
        /// </summary>
        [JsonIgnore]
        public bool IsHandled { get; set; }

        public T GetArgument<T>(string key)
        {
            return (T)this[key];
        }
    }
}
